#include <iostream>
#include <string>
#include <memory>
#include <list>
#include <algorithm>

using namespace std;

/*
\ brief:  学生对象
\ author: 清晨与猫鱼
*/
class Student {
public:

    explicit Student(string name, int rollNo) 
        : name_(move(name)), rollNo_(rollNo) {
    }

    string& getName() {
        return name_;
    }

    void setName(string name) {
        name_ = move(name);
    }

    void setRollNo(int rollNo) {
        rollNo_ = rollNo;
    }

    int getRollNo() {
        return rollNo_;
    }
private:
    string name_;
    int rollNo_;
};


/*
\ brief:   大学生
\ author: 清晨与猫鱼
*/
class StudentDao {
public:
    virtual list<Student>& getAllStudents() = 0;
    virtual Student* getStudent(int rollNo) = 0;
    virtual void updateStudent(Student& student) = 0;
    virtual void deleteStudent(Student& student) = 0;
};

/*
\ brief:  管理学生的
\ author: 清晨与猫鱼
*/
class StudentDaoImpl 
    : public StudentDao {
public:
    explicit StudentDaoImpl() {
        students_.emplace_back("Robert", 0);
        students_.emplace_back("John",1);
    } 

    list<Student>& getAllStudents() {
        return students_;
    }

    Student* getStudent(int rollNo) {
        auto it = find_if(students_.begin(), students_.end(), [&](Student& stu){
            return stu.getRollNo() == rollNo;
        });

        if (it != students_.end()) {
            return &*it;
        }

        return nullptr;
    }

    void deleteStudent(Student& student) {
        cout << "Student: Roll No " << student.getRollNo() << ", deleted from database" << endl;

        students_.remove_if([&](Student& stu) {
            return student.getRollNo() == stu.getRollNo();
        });
    }

    void updateStudent(Student& student) {
       auto it = find_if(students_.begin(), students_.end(), [&](Student& stu) {
                    return stu.getRollNo() == student.getRollNo();
                 });
        if (it != students_.end()) {
            it->setName(student.getName());
        }   
    }


private:
    list<Student> students_;
};




